package com.ruoyi.web.controller.crm;

import com.bonuo.crm.domain.Kpi;
import com.bonuo.crm.service.KpiService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

//kpi考核Controller
@RestController
@RequestMapping("/crm/kpi")
@Api(tags = "kpi考核")
public class KpiController extends BaseController {
    @Autowired
    private KpiService kpiService;

    //查询kpi考核列表
    @GetMapping("/list")
    @ApiOperation(value = "查询kpi考核列表",httpMethod = "GET")
    public TableDataInfo list(Kpi kpi,Integer pageNum,Integer pageSize){
        if(pageNum != null && pageSize != null){
            startPage();
        }
        List<Kpi> kpiList = kpiService.selectKpiList(kpi);
        return getDataTable(kpiList);
    }


    //添加kpi考核
    @PostMapping("/add")
    @ApiOperation(value = "添加kpi考核",httpMethod = "POST")
    public AjaxResult add(@RequestBody Kpi kpi){
        Integer add = kpiService.insertKpi(kpi);
        return toAjax(add);
    }

    //修改kpi考核
    @PutMapping("/update")
    @ApiOperation(value = "修改kpi考核",httpMethod = "PUT")
    public AjaxResult update(@RequestBody Kpi kpi){
        AjaxResult update = kpiService.updateKpi(kpi);
        return update;
    }

    //删除kpi考核
    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除kpi考核",httpMethod = "DELETE")
    public AjaxResult delete(@PathVariable Integer id){
        Integer delete = kpiService.deleteKpiById(id);
        return toAjax(delete);
    }

    //导出kpi考核
    @PostMapping("/export")
    @ApiOperation(value = "导出kpi考核",httpMethod = "POST")
    public void export(HttpServletResponse response , Kpi kpi){
        List<Kpi> kpiList = kpiService.selectKpiList(kpi);
        ExcelUtil<Kpi> util = new ExcelUtil<Kpi>(Kpi.class);
        util.exportExcel(response, kpiList, "kpi考核数据");
    }

    //统计各个指标数量  例如：项目收集数量
    @GetMapping("/count")
    @ApiOperation(value = "统计各个指标条数",httpMethod = "GET")
    public AjaxResult count(Kpi kpi){
        AjaxResult record = kpiService.countEveryNum(kpi);
        return record;
    }
}
